import requests


def search_papers(query, top_k=30):
    url = "http://180.184.65.98:38880/atomgit/search_papers"
#    url = "http://180.184.65.98:38880/atomgit/query_by_paper_id "
    # 设置请求参数
    params = {
        'query': query,
        'top_k': top_k
    }

    try:
        # 发起 GET 请求
        response = requests.get(url, params=params)

        # 如果响应状态码是 200，表示请求成功
        if response.status_code == 200:
            # 将响应内容解析为 JSON
            papers = response.json()
            return papers
        else:
            print(f"Error: Received status code {response.status_code}")
            return None
    except requests.RequestException as e:
        # 捕获请求异常
        print(f"An error occurred: {e}")
        return None


# 示例调用
query = "present work of 机器学习"  # 根据实际需要修改查询内容
top_k = 15  # 可选，默认为 30
papers = search_papers(query, top_k)
if papers:
    # 打印返回的论文片段信息
    for paper in papers:
        print(paper)

import re


# def replace_sup_with_sequence(article, id_list):
#     # 创建一个字典，便于快速查找检索id和对应的序号
#     id_to_sequence = []
#     for i in range(len(id_list)):
#         id_to_sequence.append([i+1, id_list[i]])
#     #print(id_to_sequence)
#     # 正则表达式匹配 <sup>...</sup> 的部分
#     sup_pattern = r'<sup>(\d+)</sup>'
#
#     # 用于记录替换信息
#     replacements = []
#
#     def replacement_func(match):
#         # 提取检索ID
#         sup_id = match.group(1)
#         #print(type(sup_id))
#         # 如果检索ID在字典中，返回对应的序号
#         match_index = len(replacements)
#         sequence, entry = id_to_sequence[match_index]
#         paper_title = entry['entity']['paper_title']
#         chunk_id = entry['entity']['chunk_id']
#         replacements.append(f"[{sequence}] {paper_title}, chunk {chunk_id}")
#
#
#         return f"<sup>{sequence}</sup>"
#
#
# print()

